package com.xh.jf_blog.controller.front;

import com.xh.jf_blog.bean.Article;
import com.xh.jf_blog.bean.Comment;
import com.xh.jf_blog.bean.Userinfo;
import com.xh.jf_blog.common.form.LoginForm;
import com.xh.jf_blog.common.form.RegisterForm;
import com.xh.jf_blog.common.util.R;
import com.xh.jf_blog.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author PangJunjie
 * @Date 2024/2/22/022
 */
@RestController
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    private UserinfoService userinfoService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private BannerService bannerService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private CodeService codeService;
    @Autowired
    private WebsiteService websiteService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private ArticleLikeService articleLikeService;

    @GetMapping("/hot/article")
    public R articleHot() {
        return R.ok(articleService.getHotArticle());
    }

    @GetMapping("/hot/author")
    public R authorHot() {
        return R.ok(userinfoService.getHotAuthor());
    }

    @GetMapping("/banner")
    public R bannerList() {
        return R.ok(bannerService.getBannerList());
    }

    @GetMapping("/category/list")
    public R categoryList() {
        return R.ok(categoryService.getList());
    }

    @GetMapping("/search")
    public R search(String value) {
        /*查询作者名称*/
        Userinfo userinfo = new Userinfo();
        userinfo.setNickname(value);
        List<Userinfo> authorList = userinfoService.getList(userinfo);
        /*查询博客标题*/
        Article article = new Article();
        article.setTitle(value);
        List<Article> titleList = articleService.getList(article);
        /*查询博客内容*/
        article = new Article();
        article.setContent(value);
        List<Article> contentList = articleService.getList(article);

        List<Article> articleList = new ArrayList<Article>(titleList);
        for (int j = 0; j < contentList.size(); j++) {
            boolean isTitle = true;
            for (int i = 0; i < titleList.size(); i++) {
                if (titleList.get(i).getArticleId() == contentList.get(j).getArticleId()) {
                    isTitle = false;
                    break;
                }
            }
            if (isTitle) {
                articleList.add(contentList.get(j));
            }
        }

        Map<String, List<?>> map = new HashMap<String, List<?>>();
        map.put("authorList", authorList);
        map.put("articleList", articleList);
        return R.ok(map);
    }

    @GetMapping("/send")
    public R send(String phone) {
        return codeService.sendMsg(phone);
    }

    @PostMapping("/register")
    public R register(RegisterForm registerForm) {
        userinfoService.register(registerForm);
        return R.ok();
    }

    @PostMapping("/login")
    public R login(LoginForm loginForm) {
        String token = tokenService.saveToken(loginForm);
        return R.ok(token);
    }


    @GetMapping("/website")
    public R info() {
        return R.ok(websiteService.getInfo());
    }

    @GetMapping("/author/page")
    public R authorPage(int page, int limit) {
        Userinfo userinfo = new Userinfo();
        userinfo.setStatus(1);
        userinfo.setRole(2);
        return userinfoService.getPage(page, limit, userinfo);
    }

    @GetMapping("/article/page")
    public R articlePageByCategoryId(int page, int limit, Article article) {
        article.setStatus(1);
        article.setArticleStatus(1);
        return articleService.getPage(page, limit, article);
    }

    @GetMapping("/article/detail/{articleId}")
    public R articleDetail(@PathVariable int articleId) {
        return R.ok(articleService.getById(articleId));
    }

    @PostMapping("/article/read/count")
    public R updateReadCount(int articleId) {
        articleService.updateReadCount(articleId);
        return R.ok();
    }

    @GetMapping("/comment/page")
    public R commentPage(int page, int limit, Comment comment) {
        comment.setStatus(1);
        return commentService.getPage(page, limit, comment);
    }

    @GetMapping("/user/info/{userinfoId}")
    public R getVipInfo(@PathVariable int userinfoId) {
        return R.ok(userinfoService.getById(userinfoId));
    }

    @GetMapping("/user/like/page")
    public R getVipLikeList(int page, int limit, int userinfoId) {
        return articleLikeService.getLikeListByUserinfoId(page, limit, userinfoId);
    }

    @GetMapping("/user/collection/page")
    public R getVipCollectionList(int page, int limit, int userinfoId) {
        return articleLikeService.getCollectionListByUserinfoId(page, limit, userinfoId);
    }

    @GetMapping("/user/follow/page")
    public R getVipFollowList(int page, int limit, int userinfoId) {
        return articleLikeService.getFollowListByUserinfoId(page, limit, userinfoId);
    }

    @GetMapping("/user/followed/page")
    public R getVipFollowedList(int page, int limit, int userinfoId) {
        return articleLikeService.getFollowedListByUserinfoId(page, limit, userinfoId);
    }

    @GetMapping("/user/comment/page")
    public R getVipCommentList(int page, int limit, int userinfoId) {
        Comment comment = new Comment();
        comment.setUserinfoId(userinfoId);
        return commentService.getPage(page, limit, comment);
    }

    @GetMapping("/user/article/page")
    public R getVipArticleList(int page, int limit, int userinfoId) {
        Article article = new Article();
        article.setUserinfoId(userinfoId);
        article.setStatus(1);
        article.setArticleStatus(1);
        return articleService.getPage(page, limit, article);
    }

    @PostMapping("/reset")
    public R resetPassword(RegisterForm registerForm) {
        userinfoService.resetPassword(registerForm);
        return R.ok();
    }
}
